<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\controller\meal;

use app\store\controller\Controller;
use app\store\model\meal\Menu as MenuModel;
use app\store\service\meal\Export as ExportService;
use think\response\Json;

/**
 * 菜单
 * Class Menu
 * @package app\store\controller\goods
 */
class Menu extends Controller
{
    public function all(): Json
    {
        // 获取列表记录
        $model = new MenuModel;
        $param = $this->request->param();

        $list = $model->getListAll($param);
        return $this->renderSuccess(compact('list'));
    }
    /**
     * 执行订单导出excel
     * @return Json
     * @throws BaseException
     */
    public function exportOrder($date = ''): Json
    {
        $model    = new ExportService;
        $filePath = $model->export($date);
        if (! $filePath) {
            $this->renderError($model->getError() ?: '导出失败');
        }
        $filePath = base_url() . $filePath;
        return $this->renderSuccess(['filePath' => $filePath], '导出Excel文件成功');
    }
    /**
     * 商品分类列表
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list($date = ''): Json
    {
        //var_dump($date);
        if ($date == '') {
            $date = Date('Y-m-d', time() + 86400);
            $can  = true;
        } else {
            $dd = explode('T', $date);
            if (isset($dd[1])) {
                $cc   = explode(';', $dd[0]);
                $date = $cc[1];
                $date = strtotime($date) + 86400;
                $date = Date('Y-m-d', $date);
            } else {

            }

            $time = strtotime($date);
            if ($time < time()) {
                $can = false;
            } else {
                $can = true;
            }
        }
        $model = new MenuModel;
        $list  = $model->getList($date);
        return $this->renderSuccess(compact('list', 'date', 'can'));
    }

    /**
     * 删除商品分类
     * @param int $categoryId
     * @return Json
     */
    public function delete(int $categoryId): Json
    {
        // 分类详情
        $model = MenuModel::detail($categoryId);
        if (! $model->remove()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 添加商品分类
     * @return Json
     */
    public function add(): Json
    {
        // 新增记录
        $model = new MenuModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑商品分类
     * @param int $categoryId
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit(string $date): Json
    {
        // 分类详情
        $model = MenuModel::detail(['date' => $date]);
        if ($model) {
            $data = $this->postForm();
            $model->edit($data);
        } else {
            // 新增记录
            $model               = new MenuModel;
            $data                = $this->postForm();
            $data['betweenTime'] = [$date];
            if ($model->add($data)) {
                return $this->renderSuccess('更新成功');
            }
        }
        return $this->renderSuccess('更新成功');

    }
}
